package com.touchportfolio.loadQueue.loaders
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import be.vandaag.events.*;
	import be.vandaag.loadQueue.IQueueLoader;

	/**
	 * The ImageLoader class is used to load an image<br>
	 * It extends the Loader class<br>
	 * <br>
	 * This class dispatches an ErrorEvent.ERROR with all the different errors that occured
	 **/
	 
	public class ImageLoader extends Loader implements IQueueLoader
	{
		private var _path:String;
		
		
		/**
		 * This is the contructor of the ImageLoader class
		 * @param is a path to the image that has to be loaded
		 **/
		
		public function ImageLoader(path:String)
		{
			_path = path;
		}
		
		/**
		 * This function is used to start the actual loading process
		 **/
		
		public function run():void
		{
			contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
			contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			load(new URLRequest(_path));
		}
		
		private function onComplete(e:Event):void
		{	
			dispatchEvent(new PayloadEvent(this,PayloadEvent.PAYLOAD));
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function IOErrorHandler(e:IOErrorEvent):void
		{
			dispatchEvent(new ErrorEvent(["Unable to load the image \""+_path+"\""], ErrorEvent.ERROR));
		}
		
	}
}